package misp_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import misp_backend.domain.*;
import misp_backend.domain.Package;
import misp_backend.service.*;
import misp_backend.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.IdUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yang
 * @description 针对表【order】的数据库操作Service实现
 * @createDate 2025-03-19 23:53:18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {
    @Autowired
    private TableService tableService;
    @Autowired
    private OrderdetailsService orderdetailsService;

    @Autowired
    private DishesService dishesService;
    @Autowired
    private PackageService packageService;
    @Autowired
    private PackagedetailsService packagedetailsService;
    private Order order;
    private BigDecimal totalprice = new BigDecimal(0);

    public Order createOrder(List<Orderdetails> orderitem, Long tableId, int customerNum) {
        if (checktable(tableId)) {
            Date time = new Date();
            order = new Order();

            String num = "so-" + IdUtil.getSnowflakeNextId();
            order.setStatus("待支付");
            order.setTableId(tableId);
            order.setOrderNum(num);
            order.setOrderlist(orderitem);
            order.setCustomerNum(customerNum);
            order.setCreateTime(time);
            this.save(order);
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_num", num);
            Table table = new Table();
            table.setStatus("使用中");
            table.setTableId(tableId);
            table.setCreateTime(time);
            tableService.updateById(table);
            Long orderid = this.getOne(queryWrapper).getOrderId();
            for (Orderdetails item : orderitem) {
                item.setStatus(0);
                item.setOrderId(orderid);
                QueryWrapper<Dishes> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("dish_id", item.getDishId());
                BigDecimal price = dishesService.getOne(queryWrapper1).getPrice();
                totalprice = totalprice.add(price.multiply(BigDecimal.valueOf(item.getQuantity())));
                orderdetailsService.save(item);
            }
            order.setTotalAmount(totalprice);
            UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_num", num).set("total_amount", totalprice);
            boolean result = this.update(updateWrapper);
            return order;
        } else return null;
    }

    public boolean checktable(Long tableId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("status", "空闲");
        Collection<Table> tables = tableService.listByMap(columnMap);
        List<Long> ids = new ArrayList<>();
        for (Table table : tables) {
            ids.add(table.getTableId());
        }
        if (ids.contains(tableId)) {
            return true;
        } else return false;
    }

    public boolean changtable(Long orderid, Long tableId) {
        if (checktable(tableId)) {
            Order order2 = this.getById(orderid);
            Long oldtableid = order2.getTableId();
            order2.setOrderId(tableId);
            UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("order_id", order2.getOrderId()).set("table_id", tableId);
            this.update(updateWrapper);
            UpdateWrapper<Table> tableUpdateWrapper = new UpdateWrapper<>();
            tableUpdateWrapper
                    .eq("table_id", oldtableid)
                    .set("status", "空闲");    // 设置为空闲
            tableService.update(tableUpdateWrapper);
            tableUpdateWrapper.clear();
            tableUpdateWrapper
                    .eq("table_id", tableId)
                    .set("status", "使用中");
            tableService.update(tableUpdateWrapper);
            return true;
        } else return false;
    }

    public List<Order> listwithname() {
        List<Order> list1 = this.list();
        List<Order> list2 = new ArrayList<>();
        for (Order order1 : list1) {
            Table table = tableService.getById(order1.getTableId());
            order1.setTableName(table.getTableName());
            list2.add(order1);
        }
        return list2;
    }

    public boolean createOrderByPackage(Long packageid, Long tableId, int customerNum) {
        boolean result=false;

      Package pack=packageService.getById(packageid);
        QueryWrapper<Packagedetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("package_id", packageid);
        List<Packagedetails> packagedetails = packagedetailsService.list(queryWrapper);
        Order order5=new Order();

        String num = "so-" + IdUtil.getSnowflakeNextId();
        order5.setStatus("待支付");
        order5.setTableId(tableId);
        order5.setCustomerNum(customerNum);
        order5.setPackageId(packageid);
        Date date=new Date();
        order5.setCreateTime(date);
        result=this.save(order5);
        QueryWrapper<Order> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("order_num", num);
        Table table = new Table();
        table.setStatus("使用中");
        table.setTableId(tableId);
        table.setCreateTime(date);
        tableService.updateById(table);
        Long orderid = this.getOne(queryWrapper2).getOrderId();
for(Packagedetails pk:packagedetails){
    Orderdetails orderdetails=new Orderdetails();
    orderdetails.setQuantity(pk.getQuantity());
    orderdetails.setDishId(Math.toIntExact(pk.getDishId()));
    orderdetails.setOrderId(orderid);
    orderdetailsService.save(orderdetails);
}
return result;
    }
}




